package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("list.html")
    public String list(SearchParam searchParam, Model model) {
        //  需要做远程调用.
        Result<Map> result = listFeignClient.list(searchParam);

        //  编写一个方法.
        String urlParam = this.makeUrlParm(searchParam);

        //  品牌面包屑传递的数据
        String trademarkParam = this.makeTradeMarkParam(searchParam.getTrademark());

        //平台属性面包屑
        List<Map> propsParamList = this.makePropsParamList(searchParam.getProps());

        //传递数据
        Map<String, Object> orderMap = this.makeOrderMap(searchParam.getOrder());

        //  存储数据：
        model.addAttribute("searchParam", searchParam);
        //  urlParam --- 拼接用户查询的条件的。
        model.addAttribute("urlParam", urlParam);
        //  trademarkParam --- 品牌面包屑
        model.addAttribute("trademarkParam", trademarkParam);
        //  propsParamList --- 平台属性面包屑
        model.addAttribute("propsParamList", propsParamList);
        //  orderMap --- 排序
        model.addAttribute("orderMap", orderMap);

        model.addAttribute(result.getData());

        //返回索引页面视图名称
        return "list/index";

    }

    //排序
    private Map<String, Object> makeOrderMap(String order) {
        Map<String, Object> map = new HashMap<>();
        //判断
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            if (split != null && split.length == 2) {
                map.put("type", split[0]);
                map.put("sort", split[1]);
            }
        } else {
            map.put("type", "1");
            map.put("sort", "desc");
        }
        return map;
    }

    //平台属性面包屑
    private List<Map> makePropsParamList(String[] props) {
        //声明集合
        List<Map> mapList =new ArrayList<>();
        //判断
        if (props != null && props.length>0){
            //循环遍历
            for (String prop : props){
                String[] split = prop.split(":");
                if (split != null && split.length==3){
                    HashMap<String,Object> hashMap = new HashMap<>();
                    hashMap.put("attrId",split[0]);
                    hashMap.put("attrValue",split[1]);
                    hashMap.put("attrName",split[2]);
                    //  将map 添加到集合中.
                    mapList.add(hashMap);
                }
            }
        }
        //  返回数据
        return mapList;
    }

    //品牌面包屑
    private String makeTradeMarkParam(String trademark) {
        //判断
        if (!StringUtils.isEmpty(trademark)){
            String[] split = trademark.split(":");
            //获取品牌名称
            if (split !=null && split.length==2){
                return "品牌:"+split[1];
            }
        }
        return null;
    }


    //拼接用户查询的条件的
    private String makeUrlParm(SearchParam searchParam) {
        //  记录拼接的url.
        StringBuilder stringBuilder = new StringBuilder();

        //  判断用户是否根据分类Id 检索.
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())) {
            stringBuilder.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())) {
            stringBuilder.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())) {
            stringBuilder.append("category3Id=").append(searchParam.getCategory3Id());
        }

        //通过关键词检索.
        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            stringBuilder.append("keyword=").append(searchParam.getKeyword());
        }

        if (!StringUtils.isEmpty(searchParam.getTrademark())) {
            if (stringBuilder.length() > 0) {
                stringBuilder.append("&trademark=").append(searchParam.getTrademark());
            }
        }

        String[] props = searchParam.getProps();
        //  判断
        if (props != null && props.length > 0) {
            //  循环遍历。
            for (String prop : props) {
                if (stringBuilder.length() > 0) {
                    stringBuilder.append("&props=").append(prop);
                }
            }
        }

        //  返回url 路径
        return "list.html?" + stringBuilder.toString();
    }
}
